package com.jiuyin.dianjing.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.model.UniversityClub;
import com.jiuyin.dianjing.ui.activity.community.CollegeMainActivity;
import com.jiuyin.dianjing.util.DisplayUtil;

import java.util.List;

public class CollegeListAdapter extends BaseAdapter {
    private Context mContext;
    private List<UniversityClub> list;

    public CollegeListAdapter(Context mContext, List<UniversityClub> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public UniversityClub getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_college, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvName.setText(getItem(position).name);
        Glide.with(mContext)
                .load(getItem(position).list_logo)
                .override(DisplayUtil.dp2px(mContext, 160), DisplayUtil.dp2px(mContext, 93.3f))
                .into(holder.ivBackground);
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, CollegeMainActivity.class);
            UniversityClub club = list.get(position);
            intent.putExtra("UniversityClub", club);
            mContext.startActivity(intent);
        });
        return convertView;
    }

    class ViewHolder {
        View itemView;
        ImageView ivBackground;
        TextView tvName;

        public ViewHolder(View itemView) {
            this.itemView = itemView;
            ivBackground = itemView.findViewById(R.id.iv_background);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}
